<?php
/**
 * @author jrodriguez
 * @copyright 2013
 */
 require_once $rootPath.'Gazzetta.DAL/Repositories/RSSSourceRepository.php';
  require_once $rootPath.'Gazzetta.DAL/Repositories/RSSCategoryRepository.php';
  require_once $rootPath.'Gazzetta.BL/RSSParser.php';
class RSSConfigManager
 {
 	protected $_repository;
 	
	function __construct() 
	{
		$this->_repository = new RSSSourceRepository();
	}
	
	public function getCategories()
	{
		$categoryRepository = new RSSCategoryRepository();
		
		return $categoryRepository->all();
	}
	
	public function addNewRSSSource($link, $title, $idCategory, $description, $enabled)
	{
		try
		{
			$parser = new RSSParser($link);
			
			if (!$this->_repository->exists($link)) 
			{
				$rssSource = new RSSSource();
				$rssSource->setLink($link);
				$rssSource->setTitle($title);
				$rssSource->setIdCategory($idCategory);
				$rssSource->setDescription($description);
				$rssSource->setEnabled($enabled);
						
				$this ->_repository->add($rssSource);
			}
		}
		catch (Exception $e)
		{
			throw new InvalidArgumentException($link.' no es un RSS valido');
		}
		
	}
	
	public function updateRSSSource($id, $link, $title, $idCategory, $description, $enabled)
	{
		$rssSource = $this->getRSSSourceById($id);
		
		$rssSource->setLink($link);
		$rssSource->setTitle($title);
		$rssSource->setIdCategory($idCategory);
		$rssSource->setDescription($description);
		$rssSource->setEnabled($enabled);
				
		$this ->_repository->update($rssSource);
	}
	
	public function deleteRSSSource($idRSSSource)
	{
		$this ->_repository->delete($idRSSSource);
	}
	
	public function getRSSSourceById($idRSSSource)
	{
		return $this ->_repository->getById($idRSSSource);
	}
	
	public function getAllRSSSources()
	{
		return $this->_repository->all();
	}
}

?>